﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OnlineManagement.Web.webs
{
    public partial class PropagateRecord : System.Web.UI.Page
    {
        int hoggeryId = 1001;
        protected void Page_Load(object sender, EventArgs e)
        {
            panSowInfo.Visible = false;
            ddlSowId.SelectedIndexChanged += new EventHandler(ddlSowId_SelectedIndexChanged);
            btnSave.Click += new EventHandler(btnSave_Click);
            btnCancel.Click += new EventHandler(btnCancel_Click);
            if (!Page.IsPostBack)
            {
                BindSowIdDrop(hoggeryId);
            }
        }

       
        private void BindSowIdDrop(int hoggeryId)
        {
            try
            {
                BLL.PropagateRecord bllPr = new BLL.PropagateRecord();
                System.Data.DataSet ds = bllPr.GetPropagateSow(hoggeryId);
                if (ds != null && ds.Tables.Count > 0)
                {
                    ddlSowId.DataSource = ds.Tables[0];
                    ddlSowId.DataTextField = "BitID";
                    ddlSowId.DataValueField = "SowID";
                    ddlSowId.DataBind();
                    ListItem item = new ListItem("--选择编号--");
                    ddlSowId.Items.Insert(0, item);

                    ddlPigType.DataSource = ds.Tables[1];
                    ddlPigType.DataTextField = "Name";
                    ddlPigType.DataValueField = "ID";
                    ddlPigType.DataBind();
                    item = new ListItem("--选择品种--");
                    ddlPigType.Items.Insert(0, item);
                }
            }
            catch (Exception ex)
            {
                Common.MessageBox.PrintMsg(panMsg, Common.MsgLevelEnum.error, ex.Message);
            }
        }

        void ddlSowId_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlSowId.SelectedIndex > 0)
            {
                BLL.PropagateRecord bllPr = new BLL.PropagateRecord();
                string feederName;
                Guid sowId;
                if (Guid.TryParse(ddlSowId.SelectedItem.Value, out sowId))
                {
                    string pigPenName = bllPr.GetSowPigPenName(hoggeryId, sowId, out feederName);
                    panSowInfo.Visible = true;
                    txtPigPenName.Text = pigPenName;
                    txtFeederName.Text = feederName;
                }
            }
            else {
                panSowInfo.Visible = false;
                txtFeederName.Text = string.Empty;
                txtPigPenName.Text = string.Empty;
            }
        }
        void btnSave_Click(object sender, EventArgs e)
        {
            Guid sowId;
            Guid pigType;
            DateTime propagateDate;
            int liveCount=0;
            int weakCount = 0;
            //畸形数
            int malformationCount=0;
            int deathCount = 0;
            if (ddlSowId.SelectedIndex <= 0 || ddlPigType.SelectedIndex <= 0)
            {
                Common.MessageBox.PrintMsg(panMsg, Common.MsgLevelEnum.attention, "请选择可繁殖的母猪或仔猪品种。");
                return;
            }
          
            Guid.TryParse(ddlSowId.SelectedItem.Value,out sowId);
            Guid.TryParse(ddlPigType.SelectedItem.Value, out pigType);

            if (!DateTime.TryParse(txtPropagateDate.Text.Trim(), out propagateDate)) {
                Common.MessageBox.PrintMsg(panMsg, Common.MsgLevelEnum.attention, "产子日期应为日期格式，请重新输入。");
                return;
            }
            int.TryParse(txtLiveCount.Text.Trim(), out liveCount);
            int.TryParse(txtWeakCount.Text.Trim(), out weakCount);
            int.TryParse(txtMalformationCount.Text.Trim(), out malformationCount);
            int.TryParse(txtDeathCount.Text.Trim(), out deathCount);

            //OnlineManagement.BLL.PropagateRecord bllPr = new BLL.PropagateRecord();
            //Model.PropagateRecord pr = new Model.PropagateRecord();
            //pr.SowID = sowId;
            //pr.HoggeryID = hoggeryId;
            //pr.PropagateDate = propagateDate;
            //pr.LiveCount = liveCount;
            //pr.DeathCount = deathCount;
            //pr.MalformationCount = malformationCount;
            //pr.WeakCount = weakCount;
            //pr.Comment = txtComment.Text.Trim();

            //string returnValue=string.Empty;
            //bool result= bllPr.AddPropagate(pr, pigType, out returnValue);
            //if (result)
            //{
            //    Common.MessageBox.PrintMsg(panMsg, Common.MsgLevelEnum.success, returnValue);
            //}
            //else {
            //    Common.MessageBox.PrintMsg(panMsg, Common.MsgLevelEnum.error, returnValue);
            //}
        }

        /// <summary>
        /// 重置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("PropagateRecord.aspx");
        }
    }
}